<?php
class XA_Db_MySQL extends XA_Db {



	public function __construct($host, $user, $password, $db_name) {
		$this->connection = new mysqli($host, $user, $password, $db_name);
		$this->connection->set_charset('utf8');
	}



	public function escapeString($string) {
		return $this->connection->real_escape_string($string);
	}



	public function query($query) {
		unset($this->insert_id);

		if (is_a($query, 'XA_Query')) {
			$query = $query->get();
		}

		if (is_object($result = $this->connection->query($query))) {
			return new XA_Data_MySQL($result);
		} elseif ($result) {
			return (($this->insert_id = $this->connection->insert_id) !== 0) ? $this->insert_id : true;
		} else {
			if (!headers_sent()) {
				header('Content-Type: text/plain');
			}

			$exception = new Exception("\n".$this->connection->error."\r\n\r\n".$query."\r\n\r\n");
			trigger_error("\n".$exception);
			die($exception);
		}
	}



   public function begin_trans() {
      $this->query('START TRANSACTION');
   }



   public function end_trans($commit = false) {
      $this->query($commit ? 'COMMIT' : 'ROLLBACK');
   }



	public function getInsertId() {
		return $this->insert_id;
	}
}
